﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Serialization;

namespace Seagull.Configuration
{
    public class SmsAlertConfig
    {
        [XmlAttribute("开启短信报警")]
        [DisplayName("开启短信报警")]
        public bool AlarmSmsEnabled { get; set; }

        [XmlAttribute("发送端口")]
        [DisplayName("发送端口")]
        public string Port { get; set; }

        [XmlAttribute("波特率")]
        [DisplayName("波特率")]
        public int Rate { get; set; }

        [DisplayName("短信接收号码")]
        [Description("号码之间使用英文逗号','分隔")]
        [XmlIgnore]
        public string SmsRecipientsAll
        {
            get
            {
                return string.Join(",", SmsRecipients);
            }
            set
            {
                if(!Config.SupressValidation)
                {
                    if (value.Any(c => !IsValidRecipientChar(c)))
                    {
                        throw new ArgumentException("短信接收号码格式错误");
                    }
                }
                SmsRecipients = value
                    .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(t => t.Trim()).ToList();
            }
        }

        private static bool IsValidRecipientChar(char c)
        {
            return Char.IsDigit(c) || c == ' ' || c == ',';
        }

        [XmlArray("短信接收号码")]
        [XmlArrayItem("号码")]
        [Browsable(false)]
        public List<string> SmsRecipients { get; set; }

        [XmlAttribute("报警发送结果检测周期分钟")]
        [DisplayName("报警发送结果检测周期分钟")]
        public int AlarmSendCheckIntervalMinutes { get; set; }
    }
}
